package com.mcp.converter.domain.rule.conversion.datatype;

import com.mcp.converter.domain.rule.conversion.ConversionRule;
import com.mcp.converter.shared.enums.IncompatibilityType;
import com.mcp.converter.shared.enums.SQLElementType;
import com.mcp.converter.domain.converter.ConversionSuggestion;
import com.mcp.converter.shared.response.sql.SQLElement;

/**
 * TIMESTAMP WITH TIME ZONE 数据类型转换规则
 * 将 TIMESTAMP WITH TIME ZONE 转换为 TIMESTAMP
 *
 * @author zengxiong
 */
public class TimestampTzToTimestampConversionRule implements ConversionRule {

    private static final String RULE_ID = "CV_DT004";
    private static final String RULE_NAME = "TIMESTAMP WITH TIME ZONE转TIMESTAMP";
    private static final String RULE_DESCRIPTION = "将TIMESTAMP WITH TIME " + "ZONE数据类型转换为TIMESTAMP" +
            "。注意：这将丢失时区信息，可能需要应用层面处理或统一存储为UTC。";

    private ConversionSuggestion conversionSuggestion;

    public TimestampTzToTimestampConversionRule() {
        this.conversionSuggestion = new ConversionSuggestion();
        this.conversionSuggestion.setSuggestion("将TIMESTAMP WITH TIME " + "ZONE替换为TIMESTAMP" +
                "。时区信息将不会在数据库层面保留。建议应用程序统一处理时区转换，或将所有时间戳存储为UTC。");
        this.conversionSuggestion.setExample("CREATE TABLE example (event_time TIMESTAMP WITH TIME ZONE) -> CREATE " + "TABLE example (event_time TIMESTAMP)");
        this.conversionSuggestion.setAutoConvertible(true);
        this.conversionSuggestion.setComplexity(ConversionSuggestion.Complexity.MEDIUM);
    }

    @Override
    public String getId() {
        return RULE_ID;
    }

    @Override
    public String getName() {
        return RULE_NAME;
    }

    @Override
    public String getDescription() {
        return RULE_DESCRIPTION;
    }

    @Override
    public IncompatibilityType getIncompatibilityType() {
        return IncompatibilityType.DATA_TYPE;
    }

    @Override
    public ConversionSuggestion getConversionSuggestion() {
        return conversionSuggestion;
    }

    @Override
    public boolean matches(Object target) {
        if (target instanceof SQLElement) {
            SQLElement element = (SQLElement) target;
            if (element.getType() == SQLElementType.DATA_TYPE) {
                String dataTypeName = element.getName() != null ? element.getName().toUpperCase() :
                        (element.getValue() != null ? element.getValue().toUpperCase() : "");
                return "TIMESTAMP WITH TIME ZONE".equals(dataTypeName);
            }
        }
        return false;
    }

    @Override
    public String convert(SQLElement element, ConversionContext context) {
        String original = element.getValue() != null ? element.getValue() : "TIMESTAMP WITH TIME ZONE";
        String converted = "TIMESTAMP";

        if (context != null) {
            context.addConversionDetail(original, converted, "TIMESTAMP WITH TIME " + "ZONE已转换为TIMESTAMP。OceanBase对TZ"
                    + "支持有限，时区信息将丢失，请在应用层面处理或统一存储为UTC。");
        }

        return converted;
    }
}
